package com.ruoyi.web.controller.miMall;

import java.util.List;

import com.bye.xiaomi_mall.domain.*;
import com.bye.xiaomi_mall.service.ITdGoodsService;
import com.bye.xiaomi_mall.service.ITdRotateImageService;
import com.bye.xiaomi_mall.service.ITdShoppingCartService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.core.domain.AjaxResult;
import com.bye.xiaomi_mall.service.ITdGoodsCategoryService;

import static com.ruoyi.Util.TokenUtil.decrypt;

@RestController
@RequestMapping("/mall")
public class IndexController {
    @Autowired
    private ITdGoodsCategoryService tdGoodsCategoryService;

    @Autowired
    private ITdRotateImageService tdRotateImageService;

    @Autowired
    private ITdGoodsService tdGoodsService;


    //    查询所有开启的类别
    @RequestMapping("/selectGoodsType")
    public AjaxResult selectGoodsType() {
        String status = "1";
        List<TdGoodsCategory> list = tdGoodsCategoryService.selectGoodsSp(status);
        return AjaxResult.success().put("typeList", list);
    }

    //查询所有轮播图
    @RequestMapping("/selectImage")
    public AjaxResult selectImage() {
        List<TdRotateImage> list = tdRotateImageService.selectTdRotateImage();
        return AjaxResult.success().put("imageList", list);
    }

    //分类查询所有主页显示的商品
    @RequestMapping("/selectMainGoods")
    public AjaxResult selectMainGoods() {
//        查询所有开启的分类
        List<TdGoodsCategory> mainGoodsList = tdGoodsCategoryService.selectGoodsSp("1");
        for (TdGoodsCategory type : mainGoodsList) {
            Long typeIdd = type.getId();
//            查看该分类所有首页展示的商品
            type.setGoodsList(tdGoodsService.selectMainGoods(typeIdd, "1"));
        }
        return AjaxResult.success().put("mainGoodsList", mainGoodsList);
    }

    //根据类别查商品
    @RequestMapping("/selectGoodsBYTypeID/{id}")
    public AjaxResult selectGoodsBYTypeID(@PathVariable("id") Long id) {

        List<TdGoods> tdGoodsList = tdGoodsService.selectMainGoods(id);

        return AjaxResult.success().put("goodsList", tdGoodsList);
    }

    //查询商品详情
    @RequestMapping("/selectGoodInfoByID/{id}")
    public AjaxResult selectGoodInfoByID(@PathVariable("id") Long id) {
        TdGoods good = tdGoodsService.selectTdGoodsById(id);
        return AjaxResult.success().put("good", good);
    }

    //根据商品名搜素商品
    @RequestMapping("/selectGoodsByName/{goodName}")
    public AjaxResult selectGoodsByName(@PathVariable("goodName") String goodName) {
        TdGoods goods = new TdGoods();
        goods.setGoodsName(goodName);
        List<TdGoods> goodsList = tdGoodsService.selectTdGoodsList(goods);
        return AjaxResult.success().put("goodsList", goodsList);
    }



}